package com.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.book.dao.UserDao;
import com.book.dto.UserPage;
import com.book.entity.User;
import com.book.service.UserService;
import com.book.utils.RedisCache;
import com.book.utils.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

//错误6001开头
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private UserDao userDao;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
     *
     * @param code 验证码
     * @param user 保存的用户对象
     * @return
     */
    @Override
    public ResponseResult editSelf(String code, User user) {
        if (!redisCache.getCacheObject(user.getEmail()).equals(code))
            return new ResponseResult(6001,"验证码不正确或失效！");
        try {
            userDao.updateById(user);
            return new ResponseResult(200,"保存成功成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(6002,"保存失败");
        }
    }

    /**
     * 获取单条用户信息
     * @param userId
     * @return
     */
    @Override
    public ResponseResult getSelf(String userId) {
        try {
            return new ResponseResult(200,"获取成功",userDao.selectById(userId));
        }catch (Exception e){
            return new ResponseResult(6003,"获取失败");
        }
    }


    /**
     * 个人修改密码
     * @param userId
     * @param code
     * @param email
     * @param pwd
     * @return
     */
    @Override
    public ResponseResult editPwd(String userId, String code, String email, String pwd) {
        try {
            if (!redisCache.getCacheObject(email).equals(code))
                return new ResponseResult(6004,"验证码不正确或失效！");
            userDao.EditPassword(userId,bCryptPasswordEncoder.encode(pwd));
            return new ResponseResult(200,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(6005,"修改密码失败");
        }
    }

    /**
     * 管理员查看用户列表
     * @param userPage
     * @return
     */
    @Override
    public ResponseResult getList(UserPage userPage) {
        try {
            QueryWrapper<User> qw = new QueryWrapper<>();
            qw.like("id",userPage.getId()).like("email",userPage.getEmail()).eq("user_type",1);
            qw.orderByAsc("status").orderByDesc("id");
            PageHelper.startPage(userPage.getCurrentPage(),userPage.getPageSize());
            List<User> userList = userDao.selectList(qw);
            PageInfo<User> userPageInfo = new PageInfo<>(userList);

            if(userPage.getCurrentPage()>userPageInfo.getPages()){
                PageHelper.startPage(userPageInfo.getPages(),userPage.getPageSize());
                userList = userDao.selectList(qw);
                userPageInfo = new PageInfo<>(userList);
            }
            return new ResponseResult(200,"获取用户数据成功",userPageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(6006,"获取用户失败");
        }
    }

    /**
     * 修改用户状态，启用还是禁用
     * @param user
     * @return
     */
    public ResponseResult updateStatus(User user){
        try {
            userDao.updateById(user);
            //修改成功后踢下线，
            redisCache.deleteObject("login:"+user.getId());
            return new ResponseResult(200,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(6007,"修改失败");
        }
    }

    /**
     * 重置密码
     * @param user
     * @return
     */
    public ResponseResult resetPwd(User user){
        try {
            //重置密码为12345678
            user.setPassword(bCryptPasswordEncoder.encode("123456789"));
            userDao.updateById(user);
            //重置密码成功后踢下线，
            redisCache.deleteObject("login:"+user.getId());
            return new ResponseResult(200,"重置密码成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(6008,"重置密码失败");
        }
    }

    /**
     * 根据id删除用户-逻辑删除
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteUser(String id) {
        try {
            userDao.deleteById(id);
            //删除成功后踢下线，
            redisCache.deleteObject("login:"+id);
            return new ResponseResult(200,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(6009,"删除失败");
        }
    }
}
